using System;
using System.Collections.Generic;
using System.Text;

namespace ClientTools.Library.Stats
{
    public class StatsManager: BaseManager
    {
        public StatsManager(string connectionString)
            : base(connectionString)
        { }

        public StatsInfo DeleteStatsByDate(DateTime date)
        {
            StatsInfo returnValue = new StatsInfo();

            System.Data.SqlClient.SqlConnection con = this.GetConnection();

            System.Data.SqlClient.SqlCommand com = new System.Data.SqlClient.SqlCommand(string.Concat("DELETE FROM umbracoStatEntry WHERE SessionID IN (SELECT ID FROM umbracoStatSession WHERE SessionStart < CONVERT(DateTime, '", date.ToString("yyyy-MM-dd"), "', 102))"), con);
            returnValue.Entries = com.ExecuteNonQuery();

            com = new System.Data.SqlClient.SqlCommand(string.Concat("DELETE FROM umbracoStatSession WHERE SessionStart < CONVERT(DateTime, '", date.ToString("yyyy-MM-dd"), "', 102)"), con);
            returnValue.Sessions = com.ExecuteNonQuery();

            return returnValue;
        }

        public StatsInfo GetStatsInfo()
        {
            // initialize the returning object
            StatsInfo returnValues = new StatsInfo();

            // initialize and open the Connection
            System.Data.SqlClient.SqlConnection con = this.GetConnection();

            System.Data.SqlClient.SqlCommand com = new System.Data.SqlClient.SqlCommand("SELECT COUNT(*) FROM umbracoStatSession", con);
            returnValues.Sessions = (int)com.ExecuteScalar();

            com = new System.Data.SqlClient.SqlCommand("SELECT COUNT(*) FROM umbracoStatEntry", con);
            returnValues.Entries = (int)com.ExecuteScalar();

            return returnValues;
        }
    }
}
